package com.jinlejia.erp.ui.customer;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.jinlejia.erp.R;
import com.jinlejia.erp.databinding.FragmentCustomerBinding;
import com.jinlejia.erp.ui.common.GuideDialog;
import com.jinlejia.erp.utils.ACache;
import com.sevenheaven.segmentcontrol.SegmentControl;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

/**
 * Created by Administrator on 2017/5/4 0004.
 * 客源
 */

public class CustomerFragment extends Fragment {

    private View view;
    private int segmentIndex = 0;
    public String isPublic = "0";

    private FragmentCustomerBinding binding;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_customer, null);
            binding = DataBindingUtil.bind(view);
            binding.btAdd.setOnClickListener(this::onClick);
            binding.btSearch.setOnClickListener(this::onClick);
            EventBus.getDefault().register(this);
            initView();
        }
        // 缓存的rootView需要判断是否已经被加过parent，
        // 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }


        FragmentManager fm = getChildFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        if (segmentIndex == 0)
            ft.replace(R.id.content, new BuyingFragment());
        else if (segmentIndex == 1)
            ft.replace(R.id.content, new MagdebrugFragment());
        ft.commitAllowingStateLoss();

        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onResume() {
        super.onResume();
        //指引弹出层
        if (TextUtils.isEmpty(ACache.get(getActivity()).getAsString("customer_enter_guide"))) {
            GuideDialog dialog = new GuideDialog(getContext(), "customer_enter_guide", R.mipmap.guide_customer_enter);
            dialog.show();
            dialog.setDissmissListener(new GuideDialog.OnDissmissListener() {
                @Override
                public void onDismiss() {
                    if (TextUtils.isEmpty(ACache.get(getActivity()).getAsString("customer_search_guide")))
                        new GuideDialog(getContext(), "customer_search_guide", R.mipmap.guide_customers_search).show();
                }
            });
        }


    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_add:
                switch (segmentIndex) {
                    case 0:
                        Intent intent = new Intent(getActivity(), RecordCustomer1Activity.class);
                        startActivity(intent);
                        break;
                    case 1:
                        Intent intent1 = new Intent(getActivity(), RecordCustomer1Activity.class);
                        startActivity(intent1);
                        break;
                    default:
                        break;
                }

                break;
            case R.id.bt_search:
                switch (segmentIndex) {
                    case 0:
                        //买卖
                        Intent intent = new Intent(getActivity(), SearchCustomerActivity.class);
                        intent.putExtra("type", "shs");
                        intent.putExtra("isPublic", isPublic);
                        startActivity(intent);
                        break;
                    case 1:
                        //租赁
                        Intent intent1 = new Intent(getActivity(), SearchCustomerActivity.class);
                        intent1.putExtra("type", "rent");
                        intent1.putExtra("isPublic", isPublic);
                        startActivity(intent1);
                        break;
                }
                break;
            default:
                break;
        }
    }

    private void initView() {
        binding.segmentControl.setOnSegmentControlClickListener(new SegmentControl.OnSegmentControlClickListener() {
            @Override
            public void onSegmentControlClick(int index) {
                segmentIndex = index;
                FragmentManager fm = getChildFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
                switch (index) {
                    case 0:
                        ft.replace(R.id.content, new BuyingFragment());
                        ft.commitAllowingStateLoss();
                        break;
                    case 1:
                        ft.replace(R.id.content, new MagdebrugFragment());
                        ft.commitAllowingStateLoss();
                        break;
                    default:
                        break;
                }
            }
        });
    }

    @Subscriber(tag = "TAG_GET_PUBLIC")
    public void getPublic(String data) {
        isPublic = data;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
